﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;

namespace Nok_Air
{
    public partial class Form : PhoneApplicationPage
    {
        private string indexAvailabilityItemsPar = "";
        private string PassengerInfoPar = "";
        private string roundtripFlag = "";
        private string FlightsXMLStringPar = "";
        private string RegisterIdPar = "";
        private string PaymentTypePar = "";
        public Form()
        {
            InitializeComponent();
            DataContext = App.ViewModel;
        }

        private void continueButton_Click(object sender, RoutedEventArgs e)
        {
            bool isEmpty = false;
            foreach (NokObjects.Passenger passenger in App.ViewModel.PassengerItem.ADULT)
            {
                if (passenger.firstname.ToString() == "") { passenger.firstnameIsEmpty = "Visible"; isEmpty = true; }
                else
                {
                    System.Text.RegularExpressions.Regex rx = new System.Text.RegularExpressions.Regex("[A-Za-z]");
                    if (rx.IsMatch(passenger.firstname.ToString()))
                    {
                        passenger.firstnameIsEmpty = "Collapsed";
                    }
                    else
                    {
                        passenger.firstnameIsEmpty = "Visible"; isEmpty = true;
                    }
                }
                if (passenger.lastname.ToString() == "") { passenger.lastnameIsEmpty = "Visible"; isEmpty = true; }
                else
                {
                    System.Text.RegularExpressions.Regex rx = new System.Text.RegularExpressions.Regex("[A-Za-z]");
                    if (rx.IsMatch(passenger.lastname.ToString()))
                    {
                        passenger.lastnameIsEmpty = "Collapsed";
                    }
                    else
                    {
                        passenger.lastnameIsEmpty = "Visible"; isEmpty = true;
                    }
                }
            }
            foreach (NokObjects.Passenger passenger in App.ViewModel.PassengerItem.CHD)
            {
                if (passenger.firstname.ToString() == "") { passenger.firstnameIsEmpty = "Visible"; isEmpty = true; }
                else
                {
                    System.Text.RegularExpressions.Regex rx = new System.Text.RegularExpressions.Regex("[A-Za-z]");
                    if (rx.IsMatch(passenger.firstname.ToString()))
                    {
                        passenger.firstnameIsEmpty = "Collapsed";
                    }
                    else
                    {
                        passenger.firstnameIsEmpty = "Visible"; isEmpty = true;
                    }
                }
                if (passenger.lastname.ToString() == "") { passenger.lastnameIsEmpty = "Visible"; isEmpty = true; }
                else
                {
                    System.Text.RegularExpressions.Regex rx = new System.Text.RegularExpressions.Regex("[A-Za-z]");
                    if (rx.IsMatch(passenger.lastname.ToString()))
                    {
                        passenger.lastnameIsEmpty = "Collapsed";
                    }
                    else
                    {
                        passenger.lastnameIsEmpty = "Visible"; isEmpty = true;
                    }
                }
            }

            //Check contact info.
            NokObjects.Contact _contact = App.ViewModel.PassengerItem.contact;
            if (_contact.country.ToString() == "") { _contact.countryIsEmpty = "Visible"; isEmpty = true; }
            else
            {
                System.Text.RegularExpressions.Regex rx = new System.Text.RegularExpressions.Regex("[A-Za-z]");
                if (rx.IsMatch(_contact.country.ToString()))
                {
                    _contact.countryIsEmpty = "Collapsed";
                }
                else
                {
                    _contact.countryIsEmpty = "Visible"; isEmpty = true;
                }
            }
            if (_contact.homephone.ToString() == "") { _contact.homephoneIsEmpty = "Visible"; isEmpty = true; }
            else
            {
                System.Text.RegularExpressions.Regex rx = new System.Text.RegularExpressions.Regex("[0-9]");
                if (rx.IsMatch(_contact.homephone.ToString()))
                {
                    _contact.homephoneIsEmpty = "Collapsed";
                }
                else
                {
                    _contact.homephoneIsEmpty = "Visible"; isEmpty = true;
                }
            }

            if (_contact.mobile.ToString() == "") { _contact.mobileIsEmpty = "Visible"; isEmpty = true; }
            else
            {
                System.Text.RegularExpressions.Regex rx = new System.Text.RegularExpressions.Regex("[0-9]");
                if (rx.IsMatch(_contact.mobile.ToString()))
                {
                    _contact.mobileIsEmpty = "Collapsed";
                }
                else
                {
                    _contact.mobileIsEmpty = "Visible"; isEmpty = true;
                }
            }

            if (_contact.email.ToString() == "") { _contact.emailIsEmpty = "Visible"; isEmpty = true; }
            else
            {
                string MatchEmailPattern = @"^(([^<>()[\]\\.,;:\s@\""]+"
      + @"(\.[^<>()[\]\\.,;:\s@\""]+)*)|(\"".+\""))@"
      + @"((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
      + @"\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+"
      + @"[a-zA-Z]{2,}))$";
                System.Text.RegularExpressions.Regex rx = new System.Text.RegularExpressions.Regex(MatchEmailPattern);
                if (rx.IsMatch(_contact.email.ToString()))
                {
                    _contact.emailIsEmpty = "Collapsed";
                }
                else
                {
                    _contact.emailIsEmpty = "Visible"; isEmpty = true;
                }
            }

            if (isEmpty)
            {
                return;
            }


            string PassengerXMLPar = "";

            /* 
             <Passenger> <title>MR</title> <firstname>JOHN</firstname> <lastname>DOE</lastname> <passcode>ADULT</passcode> </Passenger> <Passenger> <title>MR</title> <firstname>JOHNs</firstname> <lastname>DOsE</lastname> <passcode>CHD</passcode> </Passenger>
             */
            foreach (NokObjects.Passenger passenger in App.ViewModel.PassengerItem.ADULT)
            {
                PassengerXMLPar += "<Passenger>";
                PassengerXMLPar += "<title>" + passenger.title + "</title>";
                PassengerXMLPar += "<firstname>" + passenger.firstname + "</firstname>";
                PassengerXMLPar += "<lastname>" + passenger.lastname + "</lastname>";
                PassengerXMLPar += "<passcode>ADULT</passcode>";
                PassengerXMLPar += "</Passenger>";
            }
            foreach (NokObjects.Passenger passenger in App.ViewModel.PassengerItem.CHD)
            {
                PassengerXMLPar += "<Passenger>";
                PassengerXMLPar += "<title>" + passenger.title + "</title>";
                PassengerXMLPar += "<firstname>" + passenger.firstname + "</firstname>";
                PassengerXMLPar += "<lastname>" + passenger.lastname + "</lastname>";
                PassengerXMLPar += "<passcode>CHD</passcode>";
                PassengerXMLPar += "</Passenger>";
            }

            string ContactXMLPar = "";
            /* <addr1 /> <addr2 /> <city /> <state /> <country>Thailand</country> <postcode /> <mobile>6681123456</mobile> <homephone>6682123456</homephone> <officephone /> <email>MYEMAIL@GMAIL.COM</email> <agentphone />*/
           
            ContactXMLPar += "<addr1 /> <addr2 /> <city /> <state />";
            ContactXMLPar += "<country>"+ _contact.country +"</country>";
            ContactXMLPar += "<postcode />";
            ContactXMLPar += "<mobile>"+ _contact.mobile +"</mobile>";
            ContactXMLPar += "<homephone>"+ _contact.homephone +"</homephone>";
            ContactXMLPar += "<officephone />";
            ContactXMLPar += " <email>"+ _contact.email +"</email>";
            ContactXMLPar += "<agentphone />";        
            
       /*  Nok_Air.WS.NokAirServicesHelper nokSHelp = new Nok_Air.WS.NokAirServicesHelper();
            nokSHelp.SubmitBookingV2(App.ViewModel.PricingV2Items.PricingID,
                FlightsXMLStringPar, PassengerXMLPar, PaymentTypePar, App.ViewModel.PricingV2Items.TotalAmount, ContactXMLPar, RegisterIdPar);
        */
            NavigationService.Navigate(new Uri("/BookFlight/6TermsConditions.xaml?lastnamePar=" + App.ViewModel.PassengerItem.ADULT[0].lastname + "&indexAvailabilityItems=" + indexAvailabilityItemsPar + "&roundtripFlag=" + roundtripFlag + "&FlightsXMLStringPar=" + FlightsXMLStringPar + "&PassengerXMLPar=" + PassengerXMLPar + "&PaymentTypePar=" + PaymentTypePar + "&ContactXMLPar=" + ContactXMLPar + "&RegisterIdPar=" + RegisterIdPar, UriKind.Relative));
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            if (NavigationService.CanGoBack && App.ViewModel.IsFinishTransaction)
            {
                NavigationService.GoBack();
            }

            //get PassengerInfo parameter.
            if (NavigationContext.QueryString.TryGetValue("PassengerInfo", out PassengerInfoPar))
            {
                //ADULT=1,CHD=0
                string[] _PassengerInfoParArr = PassengerInfoPar.Split(',');

                int adultNum = int.Parse(_PassengerInfoParArr[0].Split('=')[1]);
                int chdNum = int.Parse(_PassengerInfoParArr[1].Split('=')[1]);
                App.ViewModel.PassengerItem = new NokObjects.PassengerItemViewModel();
                App.ViewModel.PassengerItem.ADULT = new List<NokObjects.Passenger>(adultNum);
                for (int i = 0; i < adultNum; i++)
                {
                    App.ViewModel.PassengerItem.ADULT.Add(new NokObjects.Passenger());
                }
                App.ViewModel.PassengerItem.CHD = new List<NokObjects.Passenger>(chdNum);
                for (int i = 0; i < chdNum; i++)
                {
                    App.ViewModel.PassengerItem.CHD.Add(new NokObjects.Passenger());
                }
                App.ViewModel.PassengerItem.contact = new NokObjects.Contact();
            }
            NavigationContext.QueryString.TryGetValue("indexAvailabilityItems", out indexAvailabilityItemsPar);
            NavigationContext.QueryString.TryGetValue("FlightsXMLString", out FlightsXMLStringPar);
            //get RegisterId parameter.
            NavigationContext.QueryString.TryGetValue("RegisterId", out RegisterIdPar);
            //get PaymentType parameter.
            NavigationContext.QueryString.TryGetValue("PaymentType", out PaymentTypePar);

            NavigationContext.QueryString.TryGetValue("roundtripFlag", out roundtripFlag);
        }

        private void prefix_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListPicker lp = sender as ListPicker;
            lp.Tag = lp.SelectedItem;
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox box = sender as TextBox;
            Validator.validateName(box);
        }
    }
}